package com.hutao.manage.util;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.hutao.manage.util.xmlModal.Report;
import com.hutao.manage.util.xmlModal.ReportItem;
import com.hutao.manage.util.xmlModal.ReportRoot;
import com.thoughtworks.xstream.XStream;


public class Analysis_Xml {
	
//	public static void main(String[] args) throws Exception{
//		FileReader reader = new FileReader(new File("C:\\Users\\h2oco2\\Desktop\\305\\305\\检验报告单\\检验报告单详细信息.xml"));
//		String str = "";
//		char[] c = new char[1024];
//		int length = 0;
//		while((length = reader.read(c))!=-1) {
//			str+=new String(c, 0, length);
//		}
//		
//         ReportRoot ReportRoot = (com.hutao.manage.util.xmlModal.ReportRoot) getCheckReportOrderDetailMsg(str).getData();
//         System.out.println(ReportRoot);
//		 System.out.println(ReportRoot.getReport().getReportItems());
//	}

	/**
	 * 检验报告单详细信息
	 * @param xmlStr
	 * @return
	 */
	public static Result getCheckReportOrderDetailMsg(String xmlStr) {
		try {
			Document document = DocumentHelper.parseText(xmlStr);
			Element rootElement = document.getRootElement();
			Element getReportResult = rootElement.element("Body").element("GetReportResponse").element("GetReportResult");
			 XStream xStream = new XStream();
			 //xml文档中节点映射到对应的类
			 xStream.alias("reportRoot", ReportRoot.class); 
			 xStream.alias("report", Report.class);
			 xStream.alias("reportItem", ReportItem.class);
			 //集合转换
			 xStream.addImplicitCollection(Report.class, "reportItems");
			 ReportRoot reportRoot = (ReportRoot) xStream.fromXML(getReportResult.getTextTrim());
			 return new Result(true, "解析xml成功!", reportRoot, 1);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "解析xml出错!", null, -1);
		}
		
	}
	
	/**
	 * 检验报告单id信息
	 * @param xmlStr
	 * @return
	 */
	public static Result getBasicDataIdList(String xmlStr) {
		List<Integer> idList = null;
		String code = "-1";
				
		try {
			Document document = DocumentHelper.parseText(xmlStr);
			Element rootElement = document.getRootElement();
			Element getIdsByMRNResultElement = rootElement.element("Body").element("GetIdsByMRNResponse").element("GetIdsByMRNResult");
			String xmlResult = getIdsByMRNResultElement.getTextTrim();
			
			document = DocumentHelper.parseText(xmlResult);
			rootElement = document.getRootElement();
			Element codeElement = rootElement.element("code");
			code = codeElement.getTextTrim();
			
			Element reportsElement = rootElement.element("reports");
			
			if (null != reportsElement) {
				List<Element> reportListElement = reportsElement.elements("report");
				idList = new ArrayList<>();
				
				if (reportListElement != null && reportListElement.size() > 0) {
					for (int i = 0; i < reportListElement.size(); i++) {
						Element idElement = reportListElement.get(i).element("id");
						String id = idElement.getTextTrim();
						idList.add(Integer.parseInt(id));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false, "解析基础数据xml异常", null, Integer.parseInt(code));
		}
		return new Result(true, "解析成功!", idList, Integer.parseInt(code));
	}
	
	
	
	
	
	
}



